<?php
namespace Kxdrmobile\Controller;
use Think\Controller;
class GiftController extends ExtendController {
	public function index(){
		$newgifts = S($this->sid.$this->uid."newgifts");
		if($newgifts){
			$gift_count = count($newgifts);
			$this->assign("newgifts",$newgifts);
		}
		$UGFT = D("UserGifts");
		$UGFT->sid=$this->sid;
		$UGFT->uid=$this->uid;
		$UGFT->limit="0,10";
		$gifts = $UGFT->loadMyGifts();
		$all_count = $UGFT->getAllCount();

		$this->assign("type_count",count($gifts));
		$this->assign("all_count",$all_count);
		$this->assign("gifts",$gifts);
		$this->assign("gift_count",$gift_count);
		$this->assign("ts",time());
		$this->display();
	}
	public function gift(){
		$gid = I("gid");
		$gtp = I("gtp");
		$gtpn = I("gtpn");
		$pic = I("pic");
		$name = I("name");
		
		$UGFT = D("UserGiftsView");
		$UGFT->sid = $this->sid;
		$UGFT->uid = $this->uid;
		$UGFT->gtp = $gtp;
		$UGFT->gift_id = $gid;
		$UGFT->limit = "0,10";
		$gifts = $UGFT->showMyGifts();

		if($gtp==4){
			// 读取卡密列表
			$KM = D("MainKm");
			$KM->gid = $gid;
			$KM->sid = $this->sid;
			$KM->shopuid = $this->uid;
			$km = $KM->getKmInGift();
		}
		
		$this->assign("km",$km);
		$this->assign("gid",$gid);
		$this->assign("gtp",$gtp);
		$this->assign("gtpn",$gtpn);
		$this->assign("pic",$pic);
		$this->assign("name",$name);
		$this->assign("gifts",$gifts);
		$this->display();
	}
	public function get_gift_log(){
		$gid = I("gid");
		$gtp = I("gtp");
		$index = I("index");
		
		$UGFT = D("UserGiftsView");
		$UGFT->sid = $this->sid;
		$UGFT->uid = $this->uid;
		$UGFT->gtp = $gtp;
		$UGFT->gift_id = $gid;
		$UGFT->limit = "$index+1,10";
		$gifts["gifts"] = array_values($UGFT->showMyGifts());
		$gifts["all_count"] = $UGFT->getAllCount();
		
		$this->ajaxReturn($gifts);
	}
	public function picup(){
		$egid=I("egid");
		$epid=I("epid");
		$gtp=I("gtp");
		$oid=I("oid");
		$guess_id=I("guess_id");
		$tegid=I("tegid"); //增加抽奖机会的奖品id
		$name=I("name"); //增加抽奖机会的奖品id
		$show=I("show"); //分享的回调参数，用于判断在哪个渠道分享。
		$way=I("way"); //分享的是否是天猫或淘宝

		$GIFT = D("Gifts");
				
		$GIFT->gtp = $gtp;
		$GIFT->egid = $egid;
		$GIFT->epid = $epid;
		$GIFT->oid = $oid;
		$GIFT->tegid = $tegid;
		$GIFT->name = $name;
		$GIFT->guess_id = $guess_id;
		$GIFT->sid = $this->sid;
		$GIFT->uid = $this->uid;
		$GIFT->shop_uid = $this->shop_uid;
		$GIFT->mixednick = $this->mixednick;
		$GIFT->show = $show;
		$GIFT->way = $way;
		$data = $GIFT->pickup();
		
		$this->ajaxReturn($data);
	}
	public function get_gift_jifen(){
		$egid = I("egid");
		$gtp = I("gtp");
		
		$GIFT = D("Gifts");
		$GIFT->sid = $this->sid;
		$GIFT->egid = $egid;
		$GIFT->gtp = $gtp;
		$jifen = $GIFT->getGiftJifen();
		
		$this->ajaxReturn(Array("jifen"=>$jifen));
	}
	public function get_gift_metal(){
		$egid = I("egid");
		$gtp = I("gtp");
		
		$GIFT = D("Gifts");
		$GIFT->sid = $this->sid;
		$GIFT->egid = $egid;
		$GIFT->gtp = $gtp;
		$metal = $GIFT->getGiftMetal();
		
		$this->ajaxReturn($metal);
	}
	public function get_new_gifts(){
		// $newgifts = S($this->sid.$this->uid."newgifts"); //读取缓存中的奖品。除非重设猜分数据（即清空缓存奖品），否则不再判断是否获得奖品
		if(!$newgifts)
		{
			$GIFT = D("Gifts");
			$GIFT->sid = $this->sid;
			$GIFT->uid = $this->uid;
			$newgifts = $GIFT->getWinGiftsByGuessDiff(); //使用数据库中已记录的猜分数据判断奖品
			S($this->sid.$this->uid."newgifts",$newgifts);
		}
		$re["gifts"] = $newgifts;
		
		$UGFT = D("UserGifts");
		$UGFT->sid=$this->sid;
		$UGFT->uid=$this->uid;
		$re["all_count"] = $UGFT->getAllCount();
		$this->ajaxReturn($re);
	}
	public function get_my_gifts(){
		$index = I("index");
		$UGFT = D("UserGifts");
		$UGFT->sid=$this->sid;
		$UGFT->uid=$this->uid;
		$UGFT->limit="$index+1,10";
		$gifts["gifts"] = array_values($UGFT->loadMyGifts());
		$gifts["all_count"] = $UGFT->getGroupCount();
		$this->ajaxReturn($gifts);
	}
	public function add_share_log(){
		
		$this->ajaxReturn();
	}
}